package co.edu.usbcali.adt.lists;

public class SimpleList<T> implements List<T> {
	
	private SimpleNode<T> head;

	public SimpleList() {
		super();
	}

	public SimpleList(SimpleNode<T> head) {
		super();
		this.head = head;
	}

	public SimpleNode<T> getHead() {
		return head;
	}

	public void setHead(SimpleNode<T> head) {
		this.head = head;
	}

	@Override
	public boolean add(T info) {
		SimpleNode<T> node=new SimpleNode<T>(info);
		if (head==null){
			head=node;
		}else{
			SimpleNode<T> last=getLastNode();
			last.setNext(node);
		}
		return true;
	}

	@Override
	public boolean add(int pos, T info) {
		SimpleNode<T> node=new SimpleNode<T>(info);
		if (head==null){
			head=node;
		}else{
			SimpleNode<T> last=getLastNode();
			last.setNext(node);
		}
		return true;
	}

	@Override
	public boolean addAll(List<T> list) {
		int size=list.size();
		for(int con=0;con<size;con++){
			add(list.get(con));
		}
		return true;
	}

	@Override
	public T get(int pos) {
		SimpleNode<T> node=getNode(pos);
		if(node!=null){
			return node.getInfo();
		}
		return null;
	}

	@Override
	public boolean isEmpty() {
		return (head==null);
	}

	@Override
	public T remove(int pos) {
		SimpleNode<T> node=getNode(pos-1);
		if(node!=null){
			SimpleNode<T> removedNode=node.getNext();
			node.setNext(removedNode.getNext());
			return removedNode.getInfo();
		}
		return null;
	}

	@Override
	public int size() {
		int con=0;
		SimpleNode<T> node=head;
		while(node!=null){
			node=node.getNext();
			con++;
		}
		return con;
	}
	
	/**
	 * Returns the SimpleNode that is at end of List 
	 * @return Last SimpleNode of List
	 */
	SimpleNode<T> getLastNode(){
		SimpleNode<T> node=head;
		while(node.getNext()!=null){
			node=node.getNext();
		}
		return node;
	}
	
	/**
	 * Returns SimpleNode at position pos, if element doesn't exists then
	 * returns null 
	 * @param pos Position to be returned
	 * @return SimpleNode at position pos
	 */
	SimpleNode<T> getNode(int pos){
		int con=0;
		SimpleNode<T> node=head;
		while(node!=null&&con!=pos){
			node=node.getNext();
			con++;
		}
		return node;
	}
	

}
